<template>
  <div class="bankCardInfo">
    <div class="tab_nav_bg" >
      <div  class="flex_center">
        <van-icon name="arrow-left" class="nav_left_icon" @click="$router.go(-1)"  size="18px"/>
        <div style="flex: 1;text-align: center">{{$t('添加收款银行卡')}}</div>
      </div>
    </div>
    <div class="wrapper">
      <div class="card_box" v-for="(item,index) in cardContentList" :key="index">
        <div class=" card_body ">
          <div class="cell_content_list" style="margin-top: 15px">
            <!--        收款方式-->
            <div class="cell_content_item" v-if="resourcesShow['cardIssuingBank'] === 1">
              <div style="position: relative" @click="selectPayType(index)">
                <input class="input" v-model="item.cardTypeText" :readonly="true"
                       :placeholder="$t('请选择收款方式')"/>
                <img class="input_icon" src="../../../assets/my/input_right.png">
              </div>
            </div>
            <!--        收款银行-->
            <div class="cell_content_item" v-if="resourcesShow['cardIssuingBank'] === 1">
              <div style="position: relative" @click="selectBankClick(index)">
                <input class="input" v-model="item.cardIssuingBank" :readonly="true"
                       :placeholder="$t('请选择收款银行')"/>
                <img class="input_icon" src="../../../assets/my/input_right.png">
              </div>
            </div>
            <!--        银行账户-->
            <div class="cell_content_item" v-if="resourcesShow['bankCard'] === 1">
              <input class="input" v-model="item.bankcard" type="tel"
                     oninput="value = value.replace(/[^\d]/g, '');"
                     :placeholder="$t('请输入银行账号')"/>
            </div>
            <!--        持卡人-->
            <!--            <div class="cell_content_item">-->
            <!--              <div class="text">-->
            <!--                {{ $t('持卡人') }}-->
            <!--              </div>-->
            <!--              <input class="input" readonly="true" v-model="userName"/>-->
            <!--            </div>-->

          </div>
        </div>
      </div>
      <div style="text-align: center;padding: 20px 0">
        <van-button class="confirm" type="primary" round @click="confirm">{{ $t('提交') }}</van-button>
      </div>
    </div>


    <!--   选择支付方式-->
    <van-action-sheet
        v-model:show="payTypeShow"
        :actions="[{name:$t('电子钱包'),value:1},{name:$t('银行卡'),value:2}]"
        @select="payTypeConfirm"
        :cancel-text="$t('取消')"
        close-on-click-action
    />

    <!--  选择银行卡  -->
    <van-action-sheet v-model:show="selectBankShow">
      <van-picker @confirm="selectBankChange" @cancel="selectBankShow = false"
                  :columns-field-names="{  text: 'value'}"
                  :columns="selectBankList"/>
    </van-action-sheet>
  </div>
</template>

<script>
import {onMounted, reactive, ref, toRefs} from 'vue'
import {fetchBankCard, selectCitys, addBankCard, fetchMyCard, addBankCardMultiple} from '@/api/home'
import {useI18n} from 'vue-i18n'
import {useRoute} from 'vue-router'
import {useStore} from 'vuex'
import {Dialog, Toast} from "vant";
import router from "@/router";
import {debounce, processNext, setResourcesShow} from '@/utils/tool'
import {upLoadPoint} from "@/api/my";

export default {
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  setup() {
    const {t} = useI18n()
    const store = useStore()
    const resourcesShow = ref({})
    const payTypeIndex = ref(0)
    const bankAllList = ref([])
    const cardContentList = ref([
      {
        cardIssuingBank: '',
        bankcard: '',
        cardType: null,
        cardTypeText: ''
      }
    ])
    let cardInfo = reactive({
      isSelected: false,
      userName: '',
      cardIssuingBank: '',
      bankcard: '',
      cardType: 1,
      cardTypeText: ''
    })
    const payTypeShow = ref(false)
    // 选择收款银行
    const selectBankSheet = reactive({
      selectBankShow: false,
      selectBankText: '',
      selectBankList: [],
      selectBankChange: (bankName) => {
        cardContentList.value[payTypeIndex.value].cardIssuingBank = bankName
        selectBankSheet.selectBankShow = false

      },
      selectBankClick: async (index) => {
        payTypeIndex.value = index
        const type = cardContentList.value[payTypeIndex.value].cardType
        selectBankSheet.selectBankList = type === 1 ?  bankAllList.value.filter(v=>v.bankType === 'e-wallet').map(item => item.bankName) : bankAllList.value.filter(v=>v.bankType !== 'e-wallet').map(item => item.bankName)
        selectBankSheet.selectBankShow = true
      }
    })

    function selectPayType(index) {
      payTypeIndex.value = index
      payTypeShow.value = true
    }

    const verificationInfo = () => {
      for (let i = 0; i < cardContentList.value.length; i++) {
        // 校验第一张卡是否填写完整
        if (i === 0) {
          const {cardIssuingBank, cardType, bankcard} = cardContentList.value[0]
          if (!cardType) {
            Toast(t('请选择收款方式'))
            return false
          }
          if (!cardIssuingBank) {
            Toast(t('请选择收款银行'))
            return false
          }
          if (!bankcard) {
            Toast(t('请输入银行账号'))
            return false
          }
          if (cardType === 1 && (bankcard && bankcard.length !== 11)) {
            Toast(t('钱包卡号错误'))
            return false
          }
        }
      }
      return true
    }
    const confirm = debounce(
        () => {
          if (verificationInfo()) {

            next()
          }
        }
    )
    const next = async () => {

      // 过滤没填写完银行卡
      const params = cardContentList.value.filter(item => {
        const {cardIssuingBank, cardType, bankcard} = item
        return cardIssuingBank && cardType && bankcard
      }).map((item, index) => {
        return {
          accountType: item.cardType,
          bankcard: item.bankcard,
          cardIssuingBank: item.cardIssuingBank,
          isSelected: 0,
        }
      })
      Toast.loading({
        message: t('加载中'),
        duration: 0
      })
      const res = await addBankCardMultiple(params)
      if (res.code === 1) {
        Toast({
          message: t('提交成功')
        })
        router.go(-1)
      }
    }

    function payTypeConfirm(value) {
      cardContentList.value[payTypeIndex.value].cardTypeText = value.name
      cardContentList.value[payTypeIndex.value].cardType = value.value
      if (cardContentList.value[payTypeIndex.value].cardIssuingBank) {
        cardContentList.value[payTypeIndex.value].cardIssuingBank = null
      }
    }

    function addCardContent() {
      if (cardContentList.value.length >= 4) return;
      cardContentList.value.push({
        cardIssuingBank: '',
        bankcard: '',
        cardType: null,
        cardTypeText: ''
      })
    }


    const getSelect = async () => {
      // 获取银行卡列表
      const bankCardRes = await fetchBankCard()
      if (bankCardRes.code === 1) {
        bankAllList.value = bankCardRes.data
      }
    }
    // 流程显示隐藏
    const getResourcesShow = async () => {
      resourcesShow.value = await setResourcesShow(6)
      // 清除loading
      store.dispatch('changeLoading', false)
    }

    onMounted(() => {
      getSelect()
      getResourcesShow()
    })
    return {
      ...toRefs(cardInfo),
      ...toRefs(selectBankSheet),
      cardContentList,
      bankAllList,
      payTypeIndex,
      selectPayType,
      addCardContent,
      payTypeConfirm,
      payTypeShow,
      resourcesShow,
      confirm,
      next,
    }
  }
}
</script>

<style lang="less" scoped>
.bankCardInfo {
  overflow: auto;

  .tips {
    padding: 15px 20px;
    color: #3851EC;
    font-size: 14px;
    background-color: #FFEEB4;

  }

  .add_card_btn {
    text-align: center;
    margin-top: 20px;
    color: #2A67F7;
    font-size: 14px;
  }

  .wrapper {
    padding: 20px;
    margin-top: -150px;
  }

  .dialog {
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    padding-top: 20vh;
    z-index: 999;

    .dialog_body {
      width: 90vw;
      margin: 0 auto;
      background-color: #ffffff;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
      border-radius: 3px;
      max-height: 500px;
      overflow: auto;
      padding: 0 10px;
      box-sizing: border-box;

      .list_item {
        box-sizing: border-box;
        padding: 10px;
        border-bottom: solid 1px #EEEEEE;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .image {
          height: 40px;
          width: 40px;
        }

        .text {
          color: #686868;
          font-size: 14px;
          margin-left: 10px;
          text-align: left;
          flex: 1;
        }

        .active {
          padding: 0 10px;

          img {
            height: 10px;
            width: 13px;
          }
        }
      }
    }
  }

  .card_box {
    background-color: #FFFFFF;
    box-sizing: border-box;
    border-radius: 10px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
    margin-bottom: 15px;

    .card_body {
      background-color: #FFFFFF;
      width: 100%;
      border-radius: 10px;
      padding: 10px 0;

      .card_title {
        padding: 5px 15px;
        font-size: 16px;
        font-weight: 600;
      }

      .select_pay_box {
        box-sizing: border-box;
        margin: 10px;
        border-radius: 8px;
        background-color: #F6F6F6;

        .pay_item {
          box-sizing: border-box;
          padding: 15px 10px;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .image {
            height: 36px;
            width: 36px;
          }

          .text {
            color: #686868;
            font-size: 18px;
            margin-left: 10px;
            text-align: left;
            flex: 1;
          }
        }
      }
    }
  }

  .confirm_form {
    padding: 15px 15px 0 15px;

    .confirm_item {
      margin-bottom: 15px;

      .label {
        margin-bottom: 10px;
        color: #4a4a4a;
      }

      .text {
        background: #F0EFF5;
        border-radius: 5px;
        padding: 10px;
        border-bottom: 1px solid #D7D5D5;
      }
    }
  }
}
</style>
